<?php
namespace app\api\controller;

//use app\common\controller\Common;
use app\admin\controller\Admin;
use app\api\service\R;
use think\facade\Request;
use util\jwt\Token as tokenService;
use app\common\model\Company as CompanyModel;


class Base extends Admin
{
    /**
     * 初始化方法
     * @author 陸月 <360164460@qq.com>
     */
//    protected function initialize()
//    {
//        parent::initialize();
//        // 系统开关
//        if (!config('web_site_status')) {
//            $this->error('站点已经关闭，请稍后访问~');
//        }
//    }
    static $cardType = ['FC'=>'消费卡','CC'=>'次数卡'];
    //获取单一访问令牌（签名）
    public function getSingleAccessSign(Request $request){
        if ($request::isGet()){
            $Input = $request::getInput();
            $data = json_decode($Input,true);
            $parameter = ["apikey","apisecret","timeStamp"];
            foreach ($data as $key=>$value){
                if (!in_array($key,$parameter,true)||empty($value)) return '参数错误..';
            }
            if ($re = CompanyModel::where(['status'=>1,'app_key'=>$data['apikey'],'app_secret'=>$data['apisecret']])->find()){
                $payload = array(
                    "openId"      => $re->id,
                    "apiKey"      => $data['apikey'],
                    "apiSecret"   => $data['apisecret'],
                    "request_url" => "",//请求地址
                );
                $payload = json_encode($payload);
                return tokenService::createToken($payload);
            };
            return R::error('apikey Or secret错误');
        }
        return R::error('请求类型错误');
    }

    public function twoDimensionalArray($array=[]){
        $array = json_decode($array, true);
        if (count($array) == count($array, 1)) {
            //echo '是一维数组';
            $data[]=$array;
            return $data;
        }else{
            return $array;
        }
    }

}